package com.sscaipiao.android.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.sscaipiao.android.R;
import com.sscaipiao.android.activity.service.NotificationService;
import com.sscaipiao.android.common.Constants;
import com.sscaipiao.android.model.User;
import com.sscaipiao.android.utils.ServiceUtil;

public class SecurityActivity extends Activity  implements OnClickListener {

	
	private ImageView alert;
	private ImageView cash;
	private ImageView buy;
	
	private EditText cashAmunt;
	private EditText buyAmount;
	

	
	//private LinearLayout cashTxt;
	//private LinearLayout buyTxt;
	
	private Button back;
	
	private User user;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.security);
		
		back = (Button) findViewById(R.id.security_button_back);
		alert = (ImageView) findViewById(R.id.security_image_alert);
		cash = (ImageView) findViewById(R.id.security_image_cash);
		buy = (ImageView) findViewById(R.id.security_image_buy);
		cashAmunt= (EditText)findViewById(R.id.security_edittext_cash);
		buyAmount= (EditText)findViewById(R.id.security_edittext_buy);
		//cashTxt= (LinearLayout)findViewById(R.id.security_layout_cash);
		//buyTxt= (LinearLayout)findViewById(R.id.security_layout_buy);
		
		back.setOnClickListener(this);
		alert.setOnClickListener(this);
		cash.setOnClickListener(this);
		buy.setOnClickListener(this);
		
		Intent i = this.getIntent();
		if (!i.equals(null)) {
			Bundle b = i.getExtras();
			if (b != null) {
				if (b.containsKey(Constants.USER)) {
					user = (User) b.get(Constants.USER);
				}
			}
		}
		
		SharedPreferences properties = getSharedPreferences("properties", 0);
		// set in GuideActivity's first init;
		cashAmunt.setText(properties.getString("cashNum", "20"));
		buyAmount.setText(properties.getString("buyNum", "50"));
		
		if (properties.getString("needAlert", "").equals("") || properties.getString("needAlert", "").equals("false") ) {
			alert.setImageResource(R.drawable.switch_off);
		} else {
			alert.setImageResource(R.drawable.switch_on);
		}
		
		if (properties.getString("cashLimit", "").equals("") || properties.getString("cashLimit", "").equals("false") ) {
			cash.setImageResource(R.drawable.switch_off);
			cashAmunt.setEnabled(false);
		} else {
			cash.setImageResource(R.drawable.switch_on);
			cashAmunt.setEnabled(true);
		}
		
		if (properties.getString("buyLimit", "").equals("") || properties.getString("buyLimit", "").equals("false") ) {
			buy.setImageResource(R.drawable.switch_off);
			buyAmount.setEnabled(false);
		} else {
			buy.setImageResource(R.drawable.switch_on);
			buyAmount.setEnabled(true);
		}
		
	}

	@Override
	public void onClick(View v) {
		if (R.id.security_button_back == v.getId()) {
			SecurityActivity.this.finish();
		} else if(R.id.security_image_alert == v.getId()) {
			SharedPreferences properties = getSharedPreferences("properties", 0);
			
			Intent i  = new Intent();  
            i.setClass(SecurityActivity.this, NotificationService.class); 							
            Bundle b = new Bundle();
			if (user == null) {
				user = new User();
			}

			//user.setToken(retUser.getToken());
			b.putSerializable(Constants.USER, user);
			i.putExtras(b); 
			
			if (properties.getString("needAlert", "").equals("") || properties.getString("needAlert", "").equals("false") ) {
				alert.setImageResource(R.drawable.switch_on);
				properties.edit().putString("needAlert", "true").commit();
				
				if(!ServiceUtil.isServiceRunning(this,"NotificationService")){
		            startService(i); 
				}				
				
 // call notification service
				
			} else {
				alert.setImageResource(R.drawable.switch_off);
				properties.edit().putString("needAlert", "false").commit();
				
				if(ServiceUtil.isServiceRunning(this,"NotificationService")){
		            stopService(i); 
				}
			}
		} else if(R.id.security_image_cash == v.getId()) {
			SharedPreferences properties = getSharedPreferences("properties", 0);
			
			if (properties.getString("cashLimit", "").equals("") || properties.getString("cashLimit", "").equals("false") ) {
				cash.setImageResource(R.drawable.switch_on);
				properties.edit().putString("cashLimit", "true").commit();
				cashAmunt.setEnabled(true);
			} else {
				cash.setImageResource(R.drawable.switch_off);
				properties.edit().putString("cashLimit", "false").commit();
				cashAmunt.setEnabled(false);
			}
		} else if(R.id.security_image_buy == v.getId()) {
			SharedPreferences properties = getSharedPreferences("properties", 0);
			
			if (properties.getString("buyLimit", "").equals("") || properties.getString("buyLimit", "").equals("false") ) {
				buy.setImageResource(R.drawable.switch_on);
				properties.edit().putString("buyLimit", "true").commit();
				buyAmount.setEnabled(true);
			} else {
				buy.setImageResource(R.drawable.switch_off);
				properties.edit().putString("buyLimit", "false").commit();
				buyAmount.setEnabled(false);
			}
		}
		
	}
	
	


}
